package com.lostandfound.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.lostandfound.entity.MyLogger;
import com.lostandfound.service.ItemService;
import com.lostandfound.service.LoggerService;
import com.lostandfound.service.PlateService;
import com.lostandfound.service.ThanksService;
import com.lostandfound.service.TopicService;
import com.lostandfound.util.ReturnCode;
import com.opensymphony.xwork2.ActionSupport;

@ParentPackage("basePackage")
@Namespace("/")
@Results({  
    @Result(type = "json", params = {"root", "res"})  
})
public class LoggerAction extends ActionSupport {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Autowired
	private LoggerService loggerService;
	@Autowired
	private PlateService plateService;
	@Autowired
	private ItemService itemService;
	@Autowired
	private TopicService topicService;//注入业务层实例
	@Autowired
	private ThanksService thanksService;
	
	private Map<String, Object> res = new HashMap<String, Object>();//返回json信息
	private HashMap<String, Object> pagemap;//分页信息
	
	public Map<String, Object> getRes() {
		return res;
	}
	public void setRes(Map<String, Object> res) {
		this.res = res;
	}
	public HashMap<String, Object> getPagemap() {
		return pagemap;
	}
	public void setPagemap(HashMap<String, Object> pagemap) {
		this.pagemap = pagemap;
	}
	
	@Action(value="listLoggerByPage")
	public String listLoggerByPage(){
		if(pagemap == null){//pagemap存放分页属性，如果pagemap为空，设置默认每页显示10条数据
			pagemap = new HashMap<String, Object>();
			pagemap.put("page", "1");
			pagemap.put("size", "10");
		}
		try {
			@SuppressWarnings("unchecked")
			List<MyLogger> myLoggers = (List<MyLogger>)loggerService.listLoggerByPage(pagemap);//分页查询板块信息
			int count = loggerService.getCount();
			this.res.put("myLoggers", myLoggers);//获取当页数据
			this.res.put("loggercount",count);//获取总条目
			int size = Integer.parseInt((String)pagemap.get("size"));
			this.res.put("totalpage",count%size>0?count/size+1:count/size);//获取总页数
			this.res.put("code", ReturnCode.SUCCESS);//查询成功
		} catch (Exception e) {
			System.out.println("查询失败！");
			this.res.put("code", ReturnCode.FAIL);
		}
		return SUCCESS;
	}
	
	/**
	 * 各种数据统计
	 */
	@Action(value="count")
	public String register(){
		Map<String,Object> pagemap = new HashMap<String,Object>();
		pagemap.put("pname","失物板块");
		Map<String,Object> pagemap2 = new HashMap<String,Object>();
		pagemap2.put("pname","招领板块");
		Map<String,Object> pagemap3 = new HashMap<String,Object>();
		int lostCount = topicService.Count(pagemap);
		int foundCount = topicService.Count(pagemap2);
		int thanksCount = thanksService.Count(pagemap3);
		ServletActionContext.getRequest().getSession().setAttribute("lostCount", lostCount);
		ServletActionContext.getRequest().getSession().setAttribute("foundCount", foundCount);
		ServletActionContext.getRequest().getSession().setAttribute("thanksCount", thanksCount);
		this.res.put("lostCount", lostCount);
		this.res.put("foundCount", foundCount);
		this.res.put("thanksCount", thanksCount);
		return SUCCESS;
	}

}
